package com.cooba.icoment;


import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

import com.cooba.R;
import com.cooba.icoment.message.Message;
import com.cooba.icoment.message.MessageInputStream;

import java.net.HttpURLConnection;
import java.net.URL;

/**
 * Demo
 * 
 * @author keyleduo
 */
public class Demo extends Activity{

	private static ICometClient mClient;
	private static TextView test;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.icoment_layout);
		test= (TextView) findViewById(R.id.test);
		new Thread(new Runnable() {
			@Override
			public void run() {
				refreshData();
			}
		}).start();

	}

	private void refreshData() {
		try{
			//发送数据
//			HttpURLConnection mConn=(HttpURLConnection) new URL("http://192.168.99.152:8000/push?cname=a&content=112").openConnection();
//			mConn.setRequestMethod("GET");
//			mConn.setConnectTimeout(3 * 60 * 1000);
//			mConn.setDoInput(true);
//			mConn.connect();
//			MessageInputStream mInput = new MessageInputStream(mConn.getInputStream());

			//接收数据
			mClient = ICometClient.getInstance();
			ICometConf conf = new ICometConf();
			conf.host = "192.168.99.152";
			conf.port = "8100";
			conf.url = "stream";
			conf.iConnCallback = new MyConnCallback();
			conf.iCometCallback = new MyCometCallback();
			conf.channelAllocator = new NoneAuthChannelAllocator();
			mClient.prepare(conf);
			mClient.connect();
		}catch (Exception e){

		}
	}

	public static class MyConnCallback implements IConnCallback {

		@Override
		public void onFail(String msg) {
			System.out.println("connection fail");
			System.err.println(msg);
		}

		@Override
		public void onSuccess() {
			System.out.println("connection ok");
			mClient.comet();
		}

		@Override
		public void onDisconnect() {
			System.err.println("connection has been cut off");
		}

		@Override
		public void onStop() {
			System.out.println("client has been stopped");
		}

		@Override
		public boolean onReconnect(int times) {
			System.err.println("This is the " + times + "st times.");
			if (times >= 3) {
				return true;
			}
			return false;
		}

		@Override
		public void onReconnectSuccess(int times) {
			System.out.println("onReconnectSuccess at " + times + "st time");
			mClient.comet();
		}

	}

	public static class MyCometCallback implements ICometCallback {

		@Override
		public void onDataMsgArrived(Message.Content content) {
			System.out.println("data msg arrived: " + content);
			test.setText(""+content.toString());
		}

		@Override
		public void onMsgArrived(Message msg) {
			System.out.println("msg arrived: " + msg);
		}

		@Override
		public void onErrorMsgArrived(Message msg) {
			System.err.println("error message arrived with type: " + msg.type);
		}

		@Override
		public void onMsgFormatError() {
			System.err.println("message format error");
		}

	}

	public static class NoneAuthChannelAllocator implements ChannelAllocator {

		@Override
		public Channel allocate() {
			Channel channel = new Channel();
			channel.cname = "a";
			channel.token = "token";
			channel.seq = 0;
			return channel;
		}
	}
}
